/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdministrarProgramasInternalFrame.java
 *
 * Created on 18/06/2013, 06:19:58 PM
 */
package pe.edu.unmsm.cinfo.siga.ui.ventanas;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.application.Action;
import pe.edu.unmsm.cinfo.siga.datos.Modulo;
import pe.edu.unmsm.cinfo.siga.datos.Programa;
import pe.edu.unmsm.cinfo.siga.datos.ProgramaModulo;
import pe.edu.unmsm.cinfo.siga.ui.Main;
import pe.edu.unmsm.cinfo.siga.ui.utils.EstadoRenderer;

/**
 *
 * @author alumno
 */
public class AdministrarProgramasInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form AdministrarProgramasInternalFrame */
    public AdministrarProgramasInternalFrame() {
        initComponents();
        tabularTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                firePropertyChange("programaSelected", !isProgramaSelected(), isProgramaSelected());
            }
        });
        
    }

    public Programa getProgramaSeleccionado() {
        return programaSeleccionado;
    }

    public void setProgramaSeleccionado(Programa programaSeleccionado) {
        Programa old = getProgramaSeleccionado();
        this.programaSeleccionado = programaSeleccionado;
        firePropertyChange("programaSeleccionado", old, getProgramaSeleccionado());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getResourceMap(AdministrarProgramasInternalFrame.class);
        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory(resourceMap.getString("entityManager.persistenceUnit")).createEntityManager(); // NOI18N
        consultaProgramas = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("consultaProgramas.query")); // NOI18N
        listaProgramas = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(consultaProgramas.getResultList());
        programaSeleccionado = null;
        primerButton = new javax.swing.JButton();
        anteriorButton = new javax.swing.JButton();
        modificarButton = new javax.swing.JButton();
        eliminarButton = new javax.swing.JButton();
        listadoDetalleTabPanel = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabularTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        codigoTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        estadoTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        precioContadoTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        NombreTextField = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        numPartesTextField = new javax.swing.JTextField();
        precioPartesTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        detalleTable = new javax.swing.JTable();
        actualizarButton = new javax.swing.JButton();
        ultimoButton = new javax.swing.JButton();
        siguienteButton = new javax.swing.JButton();
        crear = new javax.swing.JButton();

        setClosable(true);
        setResizable(true);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(667, 33));
        setName("Form"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getActionMap(AdministrarProgramasInternalFrame.class, this);
        primerButton.setAction(actionMap.get("primeroAction")); // NOI18N
        primerButton.setIcon(resourceMap.getIcon("primerButton.icon")); // NOI18N
        primerButton.setText(resourceMap.getString("primerButton.text")); // NOI18N
        primerButton.setName("primerButton"); // NOI18N

        anteriorButton.setAction(actionMap.get("anteriorAction")); // NOI18N
        anteriorButton.setIcon(resourceMap.getIcon("anteriorButton.icon")); // NOI18N
        anteriorButton.setText(resourceMap.getString("anteriorButton.text")); // NOI18N
        anteriorButton.setName("anteriorButton"); // NOI18N

        modificarButton.setAction(actionMap.get("modificarPrograma")); // NOI18N
        modificarButton.setIcon(resourceMap.getIcon("modificarButton.icon")); // NOI18N
        modificarButton.setName("modificarButton"); // NOI18N

        eliminarButton.setAction(actionMap.get("eliminarPrograma")); // NOI18N
        eliminarButton.setIcon(resourceMap.getIcon("eliminarButton.icon")); // NOI18N
        eliminarButton.setName("eliminarButton"); // NOI18N

        listadoDetalleTabPanel.setName("listadoDetalleTabPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabularTable.setName("tabularTable"); // NOI18N
        tabularTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaProgramas, tabularTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${proCod}"));
        columnBinding.setColumnName("Pro Cod");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${proNom}"));
        columnBinding.setColumnName("Pro Nom");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${proEst}"));
        columnBinding.setColumnName("Pro Est");
        columnBinding.setColumnClass(Short.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${proPre}"));
        columnBinding.setColumnName("Pro Pre");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${proNpar}"));
        columnBinding.setColumnName("Pro Npar");
        columnBinding.setColumnClass(Short.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${proPar}"));
        columnBinding.setColumnName("Pro Par");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programaSeleccionado}"), tabularTable, org.jdesktop.beansbinding.BeanProperty.create("selectedElement"));
        bindingGroup.addBinding(binding);

        tabularTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                irADetalle(evt);
            }
        });
        jScrollPane1.setViewportView(tabularTable);
        tabularTable.getColumnModel().getColumn(0).setMinWidth(30);
        tabularTable.getColumnModel().getColumn(0).setMaxWidth(50);
        tabularTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tabularTable.columnModel.title0")); // NOI18N
        tabularTable.getColumnModel().getColumn(1).setMinWidth(400);
        tabularTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tabularTable.columnModel.title1")); // NOI18N
        tabularTable.getColumnModel().getColumn(2).setMinWidth(30);
        tabularTable.getColumnModel().getColumn(2).setMaxWidth(50);
        tabularTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tabularTable.columnModel.title3")); // NOI18N
        tabularTable.getColumnModel().getColumn(2).setCellRenderer(new EstadoRenderer());
        tabularTable.getColumnModel().getColumn(3).setMinWidth(30);
        tabularTable.getColumnModel().getColumn(3).setMaxWidth(50);
        tabularTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tabularTable.columnModel.title5")); // NOI18N
        tabularTable.getColumnModel().getColumn(4).setMinWidth(50);
        tabularTable.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("tabularTable.columnModel.title4")); // NOI18N
        tabularTable.getColumnModel().getColumn(5).setMinWidth(50);
        tabularTable.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("tabularTable.columnModel.title2")); // NOI18N

        listadoDetalleTabPanel.addTab(resourceMap.getString("jScrollPane1.TabConstraints.tabTitle"), jScrollPane1); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jLabel1.setText("Codigo:"); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        codigoTextField.setEditable(false);
        codigoTextField.setName("codigoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programaSeleccionado.proCod}"), codigoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        bindingGroup.addBinding(binding);

        jLabel2.setText("Estado:"); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        estadoTextField.setEditable(false);
        estadoTextField.setName("estadoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programaSeleccionado.proEst}"), estadoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        bindingGroup.addBinding(binding);

        jLabel3.setText("Nombre:"); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        precioContadoTextField.setEditable(false);
        precioContadoTextField.setName("precioContadoTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programaSeleccionado.proPre}"), precioContadoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        bindingGroup.addBinding(binding);

        jLabel4.setText("Precio Contado:"); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        NombreTextField.setEditable(false);
        NombreTextField.setName("NombreTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programaSeleccionado.proNom}"), NombreTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        bindingGroup.addBinding(binding);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Por partes"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel5.setText("Precio en partes:"); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("Numero de partes:"); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        numPartesTextField.setEditable(false);
        numPartesTextField.setName("numPartesTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programaSeleccionado.proNpar}"), numPartesTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        bindingGroup.addBinding(binding);

        precioPartesTextField.setEditable(false);
        precioPartesTextField.setName("precioPartesTextField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${programaSeleccionado.proPar}"), precioPartesTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceNullValue("");
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(numPartesTextField))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(precioPartesTextField))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(numPartesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(precioPartesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jLabel7.setText("Modulos correspondiente al programa:"); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        detalleTable.setColumnSelectionAllowed(true);
        detalleTable.setName("detalleTable"); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${programaSeleccionado.programaModuloList}");
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, detalleTable);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${promOrd}"));
        columnBinding.setColumnName("Prom Ord");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${modulo.modDesc}"));
        columnBinding.setColumnName("Modulo.mod Desc");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${promEst}"));
        columnBinding.setColumnName("Prom Est");
        columnBinding.setColumnClass(Short.class);
        columnBinding.setEditable(false);
        jTableBinding.setSourceNullValue(listaProgramas);
        jTableBinding.setSourceUnreadableValue(new ArrayList<Modulo>());
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane2.setViewportView(detalleTable);
        detalleTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        detalleTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("detalleTable.columnModel.title0")); // NOI18N
        detalleTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("detalleTable.columnModel.title1")); // NOI18N
        detalleTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("detalleTable.columnModel.title2")); // NOI18N
        detalleTable.getColumnModel().getColumn(2).setCellRenderer(new EstadoRenderer());

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 776, Short.MAX_VALUE)
                    .addComponent(jLabel7)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(estadoTextField)
                            .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(precioContadoTextField))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(NombreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 346, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(NombreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(precioContadoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(codigoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(estadoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addContainerGap())
        );

        listadoDetalleTabPanel.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        actualizarButton.setIcon(resourceMap.getIcon("actualizarButton.icon")); // NOI18N
        actualizarButton.setText(resourceMap.getString("actualizarButton.text")); // NOI18N
        actualizarButton.setName("actualizarButton"); // NOI18N

        ultimoButton.setAction(actionMap.get("ultimoAction")); // NOI18N
        ultimoButton.setIcon(resourceMap.getIcon("ultimoButton.icon")); // NOI18N
        ultimoButton.setText(resourceMap.getString("ultimoButton.text")); // NOI18N
        ultimoButton.setName("ultimoButton"); // NOI18N

        siguienteButton.setAction(actionMap.get("siguienteAction")); // NOI18N
        siguienteButton.setIcon(resourceMap.getIcon("siguienteButton.icon")); // NOI18N
        siguienteButton.setText(resourceMap.getString("siguienteButton.text")); // NOI18N
        siguienteButton.setName("siguienteButton"); // NOI18N

        crear.setAction(actionMap.get("crearPrograma")); // NOI18N
        crear.setIcon(resourceMap.getIcon("crear.icon")); // NOI18N
        crear.setText(resourceMap.getString("crear.text")); // NOI18N
        crear.setName("crear"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(crear, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(modificarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 285, Short.MAX_VALUE)
                        .addComponent(primerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(anteriorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(siguienteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ultimoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(actualizarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(listadoDetalleTabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(listadoDetalleTabPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(crear, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(anteriorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(siguienteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ultimoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(actualizarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(eliminarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(modificarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(primerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {actualizarButton, anteriorButton, eliminarButton, modificarButton, siguienteButton, ultimoButton});

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void irADetalle(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_irADetalle
    if(evt.getClickCount() > 1){
        listadoDetalleTabPanel.setSelectedIndex(1);
    }
}//GEN-LAST:event_irADetalle

    @Action
    public void crearPrograma() {
        JFrame mainFrame=Main.getApplication().getMainFrame();
        NuevoProgramaDialog npd=new NuevoProgramaDialog(mainFrame, true);
        npd.setLocationRelativeTo(mainFrame);
        npd.setPrograma(new Programa());
        npd.setVisible(true);
        
        if(npd.isEntityConfirmed()){
            try {
                entityManager.getTransaction().begin();
                guardarPrograma(npd.getPrograma());
                entityManager.getTransaction().commit();
                listaProgramas.add(npd.getPrograma());
                ultimoAction();
            } catch (Exception e) {
                entityManager.getTransaction().rollback();
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
        }
    }
    
    private void guardarPrograma(Programa aGuardar){
        List<ProgramaModulo> modulos = aGuardar.getProgramaModuloList();
        aGuardar.setProgramaModuloList(Collections.EMPTY_LIST);
        entityManager.persist(aGuardar);
        aGuardar.setProgramaModuloList(modulos);
        asignarClavePrograma(aGuardar);
        entityManager.merge(aGuardar);
    }

    @Action(enabledProperty = "programaSelected")
    public void modificarPrograma() {
        int selected=tabularTable.convertRowIndexToModel(tabularTable.getSelectedRow());
        
        JFrame mainFrame=Main.getApplication().getMainFrame();
        NuevoProgramaDialog npd=new NuevoProgramaDialog(mainFrame, true);
        npd.setLocationRelativeTo(mainFrame);
        npd.setPrograma(getProgramaSeleccionado());
        
        tabularTable.clearSelection();
        setProgramaSeleccionado(new Programa());
        codigoTextField.setText("");
        NombreTextField.setText("");
        estadoTextField.setText("");
        precioContadoTextField.setText("");
        precioPartesTextField.setText("");
        numPartesTextField.setText("");
        
        npd.setVisible(true);
        if(npd.isEntityConfirmed()){
            entityManager.getTransaction().begin();
            Programa aGuardar = npd.getPrograma();
            asignarClavePrograma(aGuardar);
            entityManager.merge(aGuardar);
            entityManager.getTransaction().commit();
            listaProgramas.set(selected,npd.getPrograma());
        }else{
            entityManager.refresh(npd.getPrograma());
        }
        tabularTable.setRowSelectionInterval(selected, selected);
    }

    @Action(enabledProperty = "programaSelected")
    public void eliminarPrograma() {
        int selected=tabularTable.getSelectedRow();
        int n=JOptionPane.showConfirmDialog(null,"Esta seguro?", "Peligro",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE, null);
        if(n==JOptionPane.YES_OPTION){
            
            Programa toRemove=listaProgramas.get(tabularTable.convertRowIndexToModel(selected));
            listaProgramas.remove(toRemove);
            entityManager.getTransaction().begin();
            entityManager.getTransaction().commit();
            entityManager.remove(toRemove);
            tabularTable.clearSelection();
            
        }
    }
  
    public boolean isProgramaSelected() {
        return tabularTable.getSelectedRow()!=-1;
    }

    private void asignarClavePrograma(Programa p){
        for(ProgramaModulo pm : p.getProgramaModuloList()){
            pm.getProgramaModuloPK().setProCod(p.getProCod());
        }
    }
    
    @Action
    public void primeroAction() {
        mostrarFila(0);
    }

    @Action
    public void anteriorAction() {
        int previo = tabularTable.getSelectedRow() - 1;
        if (previo < 0) previo = 0;
        mostrarFila(previo);
    }

    @Action
    public void siguienteAction() {
        int siguiente = tabularTable.getSelectedRow() + 1;
        if(siguiente > tabularTable.getRowCount()-1) siguiente = tabularTable.getRowCount()-1;
        mostrarFila(siguiente);
    }

    @Action
    public void ultimoAction() {
        mostrarFila(tabularTable.getRowCount()-1);
    }
    
    private void mostrarFila(int fila){
        tabularTable.setRowSelectionInterval(fila, fila);
        tabularTable.scrollRectToVisible(tabularTable.getCellRect(fila, 0, true));
    }
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField NombreTextField;
    private javax.swing.JButton actualizarButton;
    private javax.swing.JButton anteriorButton;
    private javax.swing.JTextField codigoTextField;
    private javax.persistence.Query consultaProgramas;
    private javax.swing.JButton crear;
    private javax.swing.JTable detalleTable;
    private javax.swing.JButton eliminarButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JTextField estadoTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private java.util.List<Programa> listaProgramas;
    private javax.swing.JTabbedPane listadoDetalleTabPanel;
    private javax.swing.JButton modificarButton;
    private javax.swing.JTextField numPartesTextField;
    private javax.swing.JTextField precioContadoTextField;
    private javax.swing.JTextField precioPartesTextField;
    private javax.swing.JButton primerButton;
    private pe.edu.unmsm.cinfo.siga.datos.Programa programaSeleccionado;
    private javax.swing.JButton siguienteButton;
    private javax.swing.JTable tabularTable;
    private javax.swing.JButton ultimoButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
